﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Common.Utility;
using Runda.EShop.Bll;
using Runda.EShop.Common.Entity;
using System.Data;

namespace Runda.EShop.Web.Customer
{
    public partial class ProductListByKey : BasePage
    {
        public string Key
        {
            get
            {
                if (Request.QueryString["Key"] != null)
                {
                    return Request.QueryString["Key"];
                }
                return null;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid(ConstHelper.PAGE_INIT_INDEX);
            }
        }

        private void BindGrid(int PageIndex)
        {
            //MasterDataBll bll = new MasterDataBll();
            int RecordCount = 0;
            //List<BasProductEntity> list = null;
            //if (!string.IsNullOrEmpty(Key))
            //{
            //    list = bll.GetProductListByKey(Key,base.User.Identity.LoginUser.UserID.ToString(),"ProductCode DESC", AspNetPager1.PageSize, PageIndex, out RecordCount);
            //}

            CustomerProductQueryConditionEntity Condition = new CustomerProductQueryConditionEntity();
            Condition.Customer = base.User.Identity.LoginUser;
            Condition.CateType = (int)EnumHelper.CustomerProductCateType.ByFunction;
            Condition.CateCode = string.Empty;
            Condition.InstrumentID = null;
            Condition.Key = Key;

            BasProductBll bll = new BasProductBll();
            DataTable dt = bll.QueryProduct(Condition, "ProductCode DESC", AspNetPager1.PageSize, PageIndex, out RecordCount);

            gvProductList.DataSource = dt;
            gvProductList.DataBind();

            AspNetPager1.RecordCount = RecordCount;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid(AspNetPager1.CurrentPageIndex - 1);
        }

        protected void gvProductList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //Label stockStatus = e.Row.FindControl("stockStatus") as Label; 
                //DataRowView dv = e.Row.DataItem as DataRowView;
                //if (Convert.ToInt32(dv["HasStock"].ToString()) == 0)
                //{
                //    stockStatus.Text = "缺货";
                //}
                //else
                //{
                //    stockStatus.Text = "有货";
                //}

            }
        }

        protected void btnRefersh_Click(object sender, EventArgs e)
        {
            BindGrid(AspNetPager1.CurrentPageIndex - 1);
        }

        protected void gvProductList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "addto")
            {
                Guid ProductID = new Guid(e.CommandArgument.ToString());
                BizCustomerFavoriteEntity entity = new BizCustomerFavoriteEntity();
                entity.ProductID = ProductID;
                entity.CustomerID = base.User.Identity.LoginUser.UserID;
                entity.CreatedAt = DateTime.Now;
                entity.Deleted = false;

                CustomerBll bll = new CustomerBll();
                bll.AddCustomerFavorite(entity);

                ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('添加成功.');", true);
            }
        }
    }
}